package org.opens.javaskill.design.apply;

import java.util.Arrays;
import java.util.Random;

/**
 * @Date 2020/9/22 21:48
 * @Created by 99126
 */
public class WorkerThreadPattern {

    public static void main(String[] args) {
        MyChannel channel = new MyChannel(5);
        channel.startWorker();

        new TransportThread("alex1", channel).start();
        new TransportThread("alex2", channel).start();
        new TransportThread("alex3", channel).start();
    }

}

class MyChannel {

    private final static int MAX_REQUEUE = 100;

    private final MyRequest[] requestQueue;

    private int head;

    private int tail;

    private int count;

    private final WorkerThread[] workerPool;

    public MyChannel(int workers) {
        this.requestQueue = new MyRequest[MAX_REQUEUE];
        this.head = 0;
        this.tail = 0;
        this.count = 0;
        this.workerPool = new WorkerThread[workers];
        init();
    }

    public void init() {
        for (int i = 0; i < workerPool.length; i++) {
            workerPool[i] = new WorkerThread("worker " + i, this);
        }
    }

    public void startWorker() {
        Arrays.asList(workerPool).forEach(WorkerThread::start);
    }

    public synchronized void put(MyRequest request) {
        while (count >= requestQueue.length) {
            try {
                this.wait();
            } catch (InterruptedException e) {

            }
        }
        this.requestQueue[tail] = request;
        // 自动回归原点算法
        this.tail = (tail + 1) % requestQueue.length;
        this.count++;
        this.notifyAll();
    }

    public synchronized MyRequest take() {
        while (count <= 0) {
            try {
                this.wait();
            } catch (InterruptedException e) {

            }
        }
        MyRequest request = this.requestQueue[this.head];
        this.head = (this.head + 1) % this.requestQueue.length;
        this.count--;
        this.notifyAll();
        return request;
    }

}

class MyRequest {

    private final String name;

    private final int number;

    public MyRequest(String name, int number) {
        this.name = name;
        this.number = number;
    }

    public void execute() {
        System.out.println(Thread.currentThread().getName() + ": executed " + this);
    }

    @Override
    public String toString() {
        return "MyRequest{" +
                "name='" + name + '\'' +
                ", number=" + number +
                '}';
    }

}

class WorkerThread extends Thread {

    private final MyChannel channel;

    private static final Random random = new Random(System.currentTimeMillis());

    public WorkerThread(String name, MyChannel channel) {
        super(name);
        this.channel = channel;
    }

    @Override
    public void run() {
        while (true) {
            MyRequest request = channel.take();
            try {
                Thread.sleep(random.nextInt(1000));
            } catch (InterruptedException e) {

            }
            if (request != null) {
                request.execute();
            }
        }
    }
}

class TransportThread extends Thread {


    private final MyChannel channel;

    private static final Random random = new Random(System.currentTimeMillis());

    public TransportThread(String name, MyChannel channel) {
        super(name);
        this.channel = channel;
    }

    @Override
    public void run() {
        for (int i = 0; true; i++) {
            MyRequest request = new MyRequest(this.getName(), i);
            this.channel.put(request);
            try {
                Thread.sleep(random.nextInt(1000));
            } catch (InterruptedException e) {

            }
        }
    }

}